package HW.Polynomial;

/**************************************************************
* Class Polynomial
* @author Jeff Thomas - Thomas007
* @vesion 1.0.0
* Date: 02.15.2008
* Copyright 2008, Jeff Thomas
***************************************************************/

/**************************************************************
* Polynomial
* @param a - 
* @return - 
* Task:	1. 
* Side Effects: None
***************************************************************/
public class Polynomial
{
/**************************************************************
* Term inner class
* Task:	1. Create an object term with coefficient and degree
*			instance variables
* Side Effects: None
***************************************************************/

	private class Term implements Comparable<Object>
	{
		private int coefficient;
		private int degree;
		 
		public Term()
		{
			coefficient = 1;
			degree = 1;
		}
		 
		public Term(int coef, int deg)
		{
			coefficient = coef;
			degree = deg;
		}
		
		public int getCoefficient()
		{
			return coefficient;
		}
		
		public int getDegree()
		{
			return degree;
		}
		
		public void setCoefficient(int coef)
		{
			coefficient = coef;
		}
		
		public void setDegree(int deg)
		{
			degree = deg;
		}	
/**************************************************************
* toString 
* @return - Polynomial in the form 4x^4
* Task:	1. Format the Term and create correct string output
* Side Effects: None
***************************************************************/	
		public String toString()
		{
			String output = new String();
			if (coefficient == 0)
				output = "";
			else if (coefficient == 1 && degree > 1)
			{
				output = "x^" + degree;
			}
			else if (coefficient == 1 && degree == 1)
				output = "x";
			else if (degree == 0)
				output = coefficient + "";
			else if (degree == 1)
				output = coefficient + "x";
			else 
			{
				output = coefficient + "x^" + degree;
			}
			return output;
		}
/**************************************************************
* Compare To
* @param ob1 - Object to compare to
* @return - Integer 0 if equal, 1 if greater than, 2 if less
*				than.
* Task:	1. Cast the parameter ob1 as a Polynomial
*			2. Compare the two Polynomials
* Side Effects: None
***************************************************************/		
		public int compareTo(Object ob1)
		{
			Polynomial other = (Polynomial) ob1;
			if (this.getDegree() == other.getDegree())
				return 0;
			else if (this.getDegree() > other.getDegree())
				return 1;
			else
				return -1;
		}
/**************************************************************
* Add
* @param poly2 - Polynomial object to add
* @return - Polynomial that has been added
* Task:	1. Add coefficients together
* Side Effects: None
***************************************************************/		
		public Polynomial add(Polynomial poly2)
		{
			Polynomial sum = new Polynomial();
			sum.setCoefficient(this.getCoefficient() + poly2.getCoefficient());
			return sum;
		}

	}//end term inner class
	
	//being polynomial class
	private Term poly;
	
	public Polynomial()
	{
		poly = new Term();
	}
	
	public Polynomial(int coef, int deg)
	{
		poly = new Term(coef, deg);
	}
	
	public int getCoefficient()
	{
		return poly.getCoefficient();
	}
	
	public int getDegree()
	{
		return poly.getDegree();
	}
	
	public void setCoefficient(int coef)
	{
		poly.setCoefficient(coef);
	}
	
	public void setDegree(int deg)
	{
		poly.setDegree(deg);
	}
	
	public String toString()
	{
		return poly.toString();
	}
	
	public int compareTo(Object ob1)
	{
		return poly.compareTo(ob1);
	}
	
	public Polynomial add(Polynomial poly2)
	{
		return poly.add(poly2);
	}
}

/**************************************************************
* I, Jeff Thomas, certify that each part of this program was
* written by me, except as noted within the code.
*
*						_________________________
***************************************************************/